package com.liu.community.controller;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 11:26 2021/10/26
 */

import com.liu.community.dto.FileDto;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class FileController {

    @RequestMapping("/file/upload")
    @ResponseBody
    public FileDto upload( HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartHttpServletRequest.getFile("editormd-image-file");
        String filePath = "D:\\springbootTest\\community\\src\\main\\resources\\static\\images\\upload";//保存图片的路径;
        String originalFilename = file.getOriginalFilename();
        String fileName =  UUID.randomUUID() + originalFilename;
        File targetFile = new File(filePath,fileName);
        file.transferTo(targetFile);
        FileDto fileDto = new FileDto();
        fileDto.setSuccess(1);
        fileDto.setMessage("好了");
        //fileDto.setUrl("/images/my.jpg");
        //fileDto.setUrl("/images/upload/3f06e1ac-78c1-474f-aeb6-7a31e7e242b4changePassword.jpg");
        String url = "/images/upload/" + fileName;
        fileDto.setUrl(url);
        return  fileDto;
    }
}
